
package com.example.quicklaunch;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AppListAdapter extends BaseAdapter {
    private List<AppItem> mValues;
    private Context mContext;
    private LayoutInflater mInflater;

    public void setValue(List<AppItem> values) {
        this.mValues = values;
        notifyDataSetChanged();
    }

    public AppListAdapter(Context context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mValues == null ? 0 : mValues.size();
    }

    @Override
    public Object getItem(int index) {
        return mValues.get(index);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int index, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = mInflater.inflate(R.layout.app_list_item, null);
        }
        AppItem item = mValues.get(index);
        ImageView appIcon = (ImageView) view.findViewById(R.id.app_icon);
        appIcon.setImageDrawable(item.getResolveInfo()
                .loadIcon(mContext.getPackageManager()));
        String target = item.getAppLable();
        List<Integer> realHighlight = realHighlight(item.getMatchedIndex(),
                item.getBitMatchIndex(), target);
        SpannableStringBuilder builder = highlightString(realHighlight, target);
        TextView appLable = (TextView) view.findViewById(R.id.app_label);
        appLable.setText(builder);
        return view;
    }

    private SpannableStringBuilder highlightString(List<Integer> highlight, String target) {
        SpannableStringBuilder style = new SpannableStringBuilder(target);
        if (highlight == null || highlight.size() == 0) {
            return style;
        }
        int length = highlight.size();
        for (int i = 0; i < length; i += 1) {
            int start = Integer.parseInt(highlight.get(i) + "");
            int end = start + 1;
            if (start > style.length() || end > style.length()) {
                continue;
            }
            style.setSpan(new ForegroundColorSpan(Color.parseColor("#39caff")),
                    start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return style;
    }

    private List<Integer> realHighlight(List<Integer> hanziHighlight,
            List<Integer> digitHightlight, String target) {
        if (!haveLetterOrDigit(target)) {
            return hanziHighlight;
        }
        List<Integer> hanziIndex = hanziIndex(target);
        if (hanziIndex.size() == 0) {
            return digitHightlight;
        }
        List<Integer> mixedIndex = new ArrayList<Integer>();
        List<String> digits = AllAppListService.pinyin(target);
        if (hanziHighlight != null) {
            for (int hanzi : hanziHighlight) {
                int highlight = 0;
                for (int index = 0; index < hanzi; index ++) {
                    if (hanziIndex(target).indexOf(highlight) != -1) {
                        highlight += 1;
                    } else {
                        highlight += digits.get(index).length() + 1;
                    }
                }
                mixedIndex.add(highlight);
            }
        }
        if (digitHightlight != null) {
            for (int position : digitHightlight) {
                if (!indexIsHanzi(position, digits, hanziIndex)) {
                    mixedIndex.add(position);
                }
            }
        }
        return mixedIndex;
    }

    public boolean indexIsHanzi(int position, List<String> digits, List<Integer> hanziIndex) {
        if (digits.size() == 0) {
            return false;
        }
        for (int hIndex : hanziIndex) {
            int start = 0,end = 0, index = 0;
            for (;index < digits.size(); index++) {
                if (start == hIndex){
                    break;
                }
                start += digits.get(index).length() + 1;
            }
            end = start + digits.get(index == 0 ? index : index - 1).length();
            if (position > start && position < end) {
                return true;
            }
        }
        return false;
    }

    private boolean haveLetterOrDigit(String target) {
        char [] chars = target.toCharArray();
        for (char c : chars) {
            if ((c > 'a' && c < 'z')
                    ||(c > 'A' && c < 'Z')
                    ||(c > '0' && c < '9')){
                return true;
            }
        }
        return false;
    }

    private List<Integer> hanziIndex(String target) {
        List<Integer> integers = new ArrayList<Integer>();
        char [] chars = target.toCharArray();
        for (int index = 0;index < chars.length; index++) {
            char c = chars[index];
            if ((c < 'a' || c > 'z')
                    && (c < 'A' || c > 'Z')
                    && (c < '0' || c > '9')
                    && (c != ' ')){
                integers.add(index);
            }
        }
        return integers;
    }
}
